const db = require('../../config/db');
const resFormat = require('../../utils/resFormat')

function searchUser (req, res) {
  let params = req.body;
  let sql = 
  `SELECT
    * 
  FROM
    students_info 
  WHERE
    name LIKE '%${params.name || ""}%'
    AND student_number LIKE '%${params.st_num || ""}%'
    AND id_number LIKE '%${params.id_num || ""}%'
    AND birthday > '${params.minbir || new Date(0).toJSON()}'
    AND birthday < '${params.maxbir || new Date().toJSON()}'
    AND nationality LIKE '%${params.nationality || ""}%'
    AND native_place LIKE '%${params.native_place || ""}%'
    AND contact_info LIKE '%${params.contact_info || ""}%'
    AND create_time > '${params.mindate || new Date(0).toJSON()}'
    AND create_time < '${params.maxdate || new Date().toJSON()}'`
    ;

  if (params.sex !=undefined) {
    sql +=  ` AND sex = '${params.sex}'`;

  }

  console.log('sql:', sql);

  // todo 还需要添加分页处理 （后续添加）

  db.query(sql, (err, result, fields) => {
    if (err) {
      console.log(err);
      res.send(err);
    } else {
      console.log('--result--', result);
      res.send(resFormat(result));
    }
  })
}

module.exports = {
  searchUser,
}